package com.sojson.project.sys.user.controller;

import java.io.IOException;
import java.util.Set;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.sojson.project.sys.user.entity.bo.AuthcUser;
import com.sojson.result.Result;
import com.sojson.util.StringUtil;
import com.sojson.util.token.Token;
import com.sojson.util.token.TokenUtil;
import com.sojson.util.token.interfaces.BaseUserService;

import io.swagger.annotations.ApiOperation;

/**
 * 个人信息控制器接口
 * 
 * @author liu
 * @date 2021-03-16
 */
public abstract class BaseUserProfileController {

    private BaseUserService userService;

    /**
     * 获取当前Token字符串
     * 
     * @return
     * @throws Exception
     */
    @GetMapping("/getClaimsStr/{isCreate}")
    @ApiOperation("获取当前Token字符串")
    public Result<String> getClaimsStr(@PathVariable Boolean isCreate) throws Exception {
        return Result.success(TokenUtil.getClaimsStr(isCreate));
    }

    /**
     * 获取当前最新的登录用户
     * 
     * @return
     * @throws IOException 
     */
    @GetMapping("/getInfoNew")
    @PreAuthorize("@ss.login()")
    @ApiOperation(value = "获取当前最新的登录用户", response = Token.class)
    public Result<Token> getInfoNew() throws IOException {
        return Result.success(TokenUtil.refreshTokenUser(userService.findById(TokenUtil.getUserId())));
    }

    /**
     * 获取个人信息
     * 
     * @return
     * @throws IOException
     */
    @GetMapping("/getInfo")
    @PreAuthorize("@ss.login()")
    @ApiOperation(value = "获取个人信息", response = Token.class)
    public Result<Token> getInfo() throws IOException {
        Token token = TokenUtil.getToken();

        // 登录了就返回
        Result<Token> success = Result.success(token);
        success.put("roles", token.getRoles());
        success.put("permissions", token.getPermissions());

        // 角色列表字符串
        Set<String> roleNames = TokenUtil.getRoleNames(token);
        StringBuffer roleNamesStr = new StringBuffer();
        for (String string : roleNames) {
            roleNamesStr.append(string).append(",");
        }
        if (StringUtil.isNotBlankString(roleNamesStr.toString())) {
            // 角色列表字符串
            success.put("roleGroup", roleNamesStr.substring(0, roleNamesStr.length() - 1));
        }
        // 岗位列表字符串
        Set<String> postNames = TokenUtil.getRoleNames(token);
        StringBuffer postNamesStr = new StringBuffer();
        for (String string : postNames) {
            postNamesStr.append(string).append(",");
        }
        if (StringUtil.isNotBlankString(postNamesStr.toString())) {
            // 岗位列表字符串
            success.put("postGroup", postNamesStr.substring(0, postNamesStr.length() - 1));
        }
        return success;
    }

    /**
     * 通过id验证旧密码修改密码
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/updatePwdByOldPwd")
    @PreAuthorize("@ss.login()")
    @ApiOperation("通过id验证旧密码修改密码")
    public Result<Boolean> updatePwdByOldPwd(@RequestBody AuthcUser dto) throws Exception {
        if (StringUtil.isBlank(dto.getOldPwd())) {
            return Result.fail("旧密码不能为空！");
        }
        if (StringUtil.isBlank(dto.getPwd())) {
            return Result.fail("密码不能为空！");
        }

        dto.setId(TokenUtil.getUserId());
        // 修改密码
        userService.updatePwdByOldPwd(dto);
        return Result.success();
    }

    public void setUserService(BaseUserService userService) {
        this.userService = userService;
    }

    /**
     * 初始化用户Service
     */
    protected abstract void initUserService();

}